#import "MingPianUI.h"
# import "ConfigData.h"
#import "AFNetworking.h"
@interface MingPianUI ()

@end

@implementation MingPianUI
@synthesize weight,qujian_time,bdImgPath,serverPath,avatar,addAddressBtn;
@synthesize qm,city_name;
@synthesize nickname,hometownBtn;
//家乡
-(void)showHomeTownProvinceFn{
    int  h=[Tools getScreenHeight];
    
    @try{
        if(true)
        {
            if(toolbar){
                [toolbar removeFromSuperview];
            }
            toolbar = [[UIToolbar alloc]initWithFrame: CGRectMake(0, 68, 320, 44)];
            
            UIImage* bg=[UIImage imageNamed:@"navigation@2x"];
            UIImageView* bgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            bgView.image=bg;
            [toolbar addSubview:bgView];
            
            
            UIBarButtonItem *btn1 = [[ UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone
                                                                    target: self action: @selector(hideHomePickerView)];
            btn1.tintColor=[UIColor whiteColor];
            
            UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            
            
            UIBarButtonItem *btn3 = [[ UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone
                                                                     target: self action: @selector(ShowHomeLabel)];
            btn3.tintColor=[UIColor whiteColor];
            
            
            
            NSMutableArray *barItems = [[NSMutableArray alloc] init];
            [barItems addObject:btn1];
            [barItems addObject:btn2];
            [barItems addObject:btn3];
            
            
            [toolbar setItems:barItems animated:YES];
            
            
            
            if (provincePicker) {
                [provincePicker.view removeFromSuperview];
            }
            if(homeTownPicker){
                [homeTownPicker.view removeFromSuperview];
            }
            homeTownPicker = [[ProvinceCityPicker alloc]init];
            homeTownPicker.view.frame =CGRectMake(0, 155, 320, 460);
            if ([Tools getIOSVersion]>=7.0) {
                homeTownPicker.view.frame =CGRectMake(0, 100, 320, h-100);
            }
            homeTownPicker.view.backgroundColor=[UIColor whiteColor];
            
            
            
            [self.view addSubview: homeTownPicker.view];
            
            
            [self.view addSubview:toolbar];
            [UIView beginAnimations: @"Animation" context:nil];
            [UIView setAnimationDuration:0.1];
            
            toolbar.frame = CGRectMake(0, 111, 320, 44);
            if ([Tools getIOSVersion]>=7.0) {
                toolbar.frame = CGRectMake(0, 68, 320, 44);
            }
            
            [UIView commitAnimations];
            
        }
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
    
}

-(void)ShowHomeLabel{
    homeprovince=homeTownPicker.province;
    NSLog(@"homeprovince=%@",homeTownPicker.province);
    
    homecity=homeTownPicker.city;
    NSLog(@"homecity=%@",homeTownPicker.city);
    
    homezone=homeTownPicker.zone;
    NSLog(@"homezone=%@",homeTownPicker.zone);
    
    if(toolbar){
        [toolbar removeFromSuperview];
    }
    
    if(provincePicker){
        [provincePicker.view removeFromSuperview];
    }
    if(homeTownPicker){
        [homeTownPicker.view removeFromSuperview];
    }
    
    
    UILabel* addrLb=[[UILabel alloc] initWithFrame:CGRectMake(10,0, 280,50)];
    addrLb.backgroundColor=[UIColor clearColor];
    
    NSString* tmp=homeprovince;
    
    if([homecity isEqualToString:@"北京"]||[homecity isEqualToString:@"上海"]){
        
    }else{
        tmp=[homeprovince stringByAppendingString:homecity];
    }
    
    
    tmp=[tmp stringByAppendingString:homezone];
    addrLb.text=tmp;
    addrLb.textColor=[UIColor blackColor];
    NSArray* subViews=[self.hometownBtn subviews];
    for (UIView* v in subViews) {
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [self.hometownBtn addSubview:addrLb];
}


////////////////////////////////////////////////////////////////////
-(void)ShowProvinceLabel{
    province=provincePicker.province;
    NSLog(@"province=%@",provincePicker.province);
    
    city=provincePicker.city;
    NSLog(@"city=%@",provincePicker.city);
    
    zone=provincePicker.zone;
    NSLog(@"zone=%@",provincePicker.zone);
    
    if(toolbar){
        [toolbar removeFromSuperview];
    }
    
    if(provincePicker){
        [provincePicker.view removeFromSuperview];
    }
    
    
    UILabel* addrLb=[[UILabel alloc] initWithFrame:CGRectMake(10,0, 280,50)];
    addrLb.backgroundColor=[UIColor clearColor];
    
    NSString* tmp=province;
    
    if([city isEqualToString:@"北京"]||[city isEqualToString:@"上海"]){
        
    }else{
        tmp=[province stringByAppendingString:city];
    }
    
    
    tmp=[tmp stringByAppendingString:zone];
    addrLb.text=tmp;
    addrLb.textColor=[UIColor blackColor];
    NSArray* subViews=[self.addAddressBtn subviews];
    for (UIView* v in subViews) {
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    [self.addAddressBtn addSubview:addrLb];
}

-(void)loadMyInfoFn{
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
   NSString* mp_ = [accountDefaults objectForKey:@"phone"];
    //NSString* mp_=[ud objectForKey:@"mp"];
    if (!mp_) {
        return;
    }
    NSLog(@"mp:%@",mp_);
    NSString* loginURL=HOST;
    loginURL=[loginURL stringByAppendingString:@"index.php/AppUser/loadUserInfoByMp?mp="];
    loginURL=[loginURL stringByAppendingFormat:@"%@",mp_];
    NSLog(@"保存URL.loginURL==%@",loginURL);
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    
    
    [req startSynchronous];
    NSError *error = [req error];
    
    
    if (!error) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [MBProgressHUD hideHUDForView:app.window animated:YES];
        
        NSString *response = [req responseString];
        NSLog(@"保存返回结果%@",response);
        myInfoDic=[response JSONValue];
        NSString* uid=[myInfoDic objectForKey:@"uid"];
        NSLog(@"ID%@",uid);
        
        
        
        [self performSelectorOnMainThread:@selector(finishLoadMyInfoFn) withObject:nil waitUntilDone:YES];
    }else{
        NSLog(@"move failed:%@", [error localizedDescription]);
    }
    
//    NSString* strURL=[NSString stringWithFormat:@"%@%@",HOST,@"index.php/AppUser/loadUserInfoByMp"];
//    
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    
//    //2.设定类型. (这里要设置request-response的类型)
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    
//    //2.设置登录参数
//    NSDictionary *dict = @{@"mp":mp,
//                           };
//    
//    //3.发送请求
//    [manager POST:strURL parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"postjson--> %@", operation.request.URL);  //这样显示JSON的话需要设置text/plain
//        NSLog(@"response:%@",responseObject);
//        //[self showResult:responseObject];
//    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@", error);
//        //        if (pageCount == 0) {
//        //            __itemsDic = [NSMutableArray new];
//        //            [mainTableView.header endRefreshing];
//        //        }else{
//        //            [mainTableView.footer endRefreshing];
//        //        }
//    }];
    
    
    
    
}
-(void)finishLoadMyInfoFn{
    self.start_addr=myInfoDic[@"detail_address"];
    startAddrTF.text=self.start_addr;
    [table reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSString*  u=[ud  objectForKey:@"u"];
    u=[NSString stringWithFormat:@"%@",u];
    if (u.length==11) {
        self.mp=u;
    }else{
        self.mp=@"";
    }
    NSLog(@"viewWillAppear.mp===%@",self.mp);
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
    
    
    
    
}




-(void)hideArrivePicker{
    [toolbar removeFromSuperview];
    [picker.view removeFromSuperview];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
}
-(void)ShowArriveLabel{
    NSMutableString* tmp=[[NSMutableString alloc] init];
    month=picker.month;
    NSLog(@"month=%@",picker.month);
    [tmp appendString:month];
    
    NSLog(@"city=%@",picker.day);
    [tmp appendString:picker.day];
    month=picker.month;
    
    [toolbar removeFromSuperview];
    
    
    
    NSArray* subViews=[arriveTimeBtn subviews];
    for (UIView* v in subViews) {
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    UILabel* qujianTimeLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 50)];
    qujianTimeLb.text=tmp;
    arrive_time=tmp;
    [arriveTimeBtn addSubview:qujianTimeLb];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
    
}



-(void)showArriveTimeFn{
    
    int h=[Tools getScreenHeight];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideTab" object:nil];
    @try{
        if(true)
        {
            if(toolbar){
                [toolbar removeFromSuperview];
            }
            
            toolbar = [[UIToolbar alloc]initWithFrame: CGRectMake(0, 68, 320, 44)];
            
            toolbar.barStyle = UIBarStyleBlackTranslucent;
            
            UIBarButtonItem *btn1 = [[ UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone
                                                                    target: self action: @selector(hideArrivePicker)];
            UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            
            
            UIBarButtonItem *btn3 = [[ UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone
                                                                     target: self action: @selector(ShowArriveLabel)];
            
            
            
            NSMutableArray *barItems = [[NSMutableArray alloc] init];
            [barItems addObject:btn1];
            [barItems addObject:btn2];
            [barItems addObject:btn3];
            
            
            [toolbar setItems:barItems animated:YES];
            
            
            if (picker) {
                [picker.view removeFromSuperview];
            }
            
            
            picker = [[BirthdayUI alloc]init];
            picker.view.frame =CGRectMake(0, 155, 320, 460);
            if ([Tools getIOSVersion]>=7.0) {
                picker.view.frame =CGRectMake(0, 100, 320, h-100);
            }
            picker.view.backgroundColor=[UIColor whiteColor];
            
            [self.view addSubview: picker.view];
            
            
            [self.view addSubview:toolbar];
            [UIView beginAnimations: @"Animation" context:nil];
            [UIView setAnimationDuration:0.1];
            
            toolbar.frame = CGRectMake(0, 111, 320, 44);
            if ([Tools getIOSVersion]>=7.0) {
                toolbar.frame = CGRectMake(0, 68, 320, 44);
            }
            
            [UIView commitAnimations];
            
        }
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
    
}









-(void)hidePickerView{
    if(toolbar){
        [toolbar removeFromSuperview];
    }
    if(homeTownPicker){
        [homeTownPicker.view removeFromSuperview];
    }
    if(picker){
        [picker.view removeFromSuperview];
    }
    
    
}
-(void)hideHomePickerView{
    if(toolbar){
        [toolbar removeFromSuperview];
    }
    if(homeTownPicker){
        [homeTownPicker.view removeFromSuperview];
    }
    if(picker){
        [picker.view removeFromSuperview];
    }
    
    
}



-(void)ShowLabel{
    NSMutableString* tmp=[[NSMutableString alloc] init];
    month=picker.month;
    NSLog(@"month=%@",picker.month);
    [tmp appendString:month];
    
    NSLog(@"city=%@",picker.day);
    [tmp appendString:picker.day];
    month=picker.month;
    NSLog(@"hour=%@",picker.hour);
    [tmp appendString:picker.hour];
    
    [toolbar removeFromSuperview];
    [picker.view removeFromSuperview];
    
    
    NSArray* subViews=[qujianTF subviews];
    for (UIView* v in subViews) {
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    UILabel* qujianTimeLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 50)];
    qujianTimeLb.text=tmp;
    qujian_time=tmp;
    [qujianTF addSubview:qujianTimeLb];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
}
-(void)showProvinceFn{
    int  h=[Tools getScreenHeight];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hideTab" object:nil];
    @try{
        if(true)
        {
            if(toolbar){
                [toolbar removeFromSuperview];
            }
            toolbar = [[UIToolbar alloc]initWithFrame: CGRectMake(0, 68, 320, 44)];
            
            UIImage* bg=[UIImage imageNamed:@"navigation@2x"];
            UIImageView* bgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            bgView.image=bg;
            [toolbar addSubview:bgView];
            
            
            UIBarButtonItem *btn1 = [[ UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone
                                                                    target: self action: @selector(hidePickerView)];
            btn1.tintColor=[UIColor whiteColor];
            
            UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            
            
            UIBarButtonItem *btn3 = [[ UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone
                                                                     target: self action: @selector(ShowProvinceLabel)];
            btn3.tintColor=[UIColor whiteColor];
            
            
            
            NSMutableArray *barItems = [[NSMutableArray alloc] init];
            [barItems addObject:btn1];
            [barItems addObject:btn2];
            [barItems addObject:btn3];
            
            
            [toolbar setItems:barItems animated:YES];
            
            
            
            if (provincePicker) {
                [provincePicker.view removeFromSuperview];
            }
            provincePicker = [[ProvinceCityPicker alloc]init];
            provincePicker.view.frame =CGRectMake(0, 155, 320, 460);
            if ([Tools getIOSVersion]>=7.0) {
                provincePicker.view.frame =CGRectMake(0, 100, 320, h-100);
            }
            provincePicker.view.backgroundColor=[UIColor whiteColor];
            
            
            
            [self.view addSubview: provincePicker.view];
            
            
            [self.view addSubview:toolbar];
            [UIView beginAnimations: @"Animation" context:nil];
            [UIView setAnimationDuration:0.3];
            
            toolbar.frame = CGRectMake(0, 111, 320, 44);
            if ([Tools getIOSVersion]>=7.0) {
                toolbar.frame = CGRectMake(0, 68, 320, 44);
            }
            
            [UIView commitAnimations];
            
        }
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
    
}

-(void)clickOnBackBut{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [NSThread detachNewThreadSelector:@selector(loadMyInfoFn) toTarget:self withObject:nil];
    
    //快递类型
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setKDTypeDataFn:) name:@"setKDTypeDataFn" object:nil];
    //setTodayOrTomorrowFn
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setTodayOrTomorrowFn:) name:@"setTodayOrTomorrowFn" object:nil];
    
    
    
    
    UILabel* titleLb=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
    titleLb.text=@"我的名片";
    titleLb.backgroundColor=[UIColor clearColor];
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=titleLb;
    
    int h=[Tools getScreenHeight];
    int w=[Tools getScreenWidth];
    table=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
    table.delegate=self;
    table.dataSource=self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    
    
    
    
    UIButton* cancelBtn= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    [cancelBtn addTarget:self action:@selector(clickOnBackBut) forControlEvents:UIControlEventTouchUpInside];
    //
    CGRect frame_1= CGRectMake(0, 7, 40, 30);
    UIImageView*  backView=[[UIImageView alloc] initWithFrame:frame_1];
    UIImage* image= [UIImage imageNamed:@"back2015.png"];
    backView.image=image;
    [cancelBtn addSubview:backView];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    
    
}
#pragma mark - table
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        return 70;
    }
    else if(indexPath.row==8){
        return 70;
    }
    return 55;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 19;
}
-(void)saveOrderFn{
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSString*  uid=[ud objectForKey:@"uid"];
    
    int _currentRow=0;
    self.nickname=nicknameTF.text;
    
    
    if (!nickname) {
        _currentRow=0;
        
        [self.view makeToast:@"请选择快递类型" duration:1.0 position:@"center"];
        [table selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0]
                           animated:YES
                     scrollPosition:UITableViewScrollPositionMiddle];
        
        return;
        
    }
    
    
    
    
    
    
    if(!uid){
        
        LoginUI*  ui=[[LoginUI alloc] init];
        ui.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:ui animated:NO];
    }else{
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [[MBProgressHUD showHUDAddedTo:app.window animated:YES] setLabelText:@"处理中,请稍后..."];
        [NSThread detachNewThreadSelector:@selector(doSaveOrderFn) toTarget:self withObject:nil];
        
    }
    
    
}
-(void)doSaveOrderFn{
    //kdType
    
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSString*  longi=[ud objectForKey:@"longi"];
    NSString*  lati=[ud objectForKey:@"lati"];
    
    NSString* loginURL=HOST;
    loginURL=[loginURL stringByAppendingString:@"index.php/AppOrder/saveOrder"];
    NSLog(@"保存URL.loginURL==%@",loginURL);
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    //
    [req addPostValue:[NSString stringWithFormat:@"%@",longi] forKey:@"longi"];
    [req addPostValue:[NSString stringWithFormat:@"%@",lati] forKey:@"lati"];
    
    [req addPostValue:self.gender forKey:@"gender"];//快递类型
    
    [req addPostValue:self.height forKey:@"height"];
    [req addPostValue:self.start_addr forKey:@"start_addr"];
    
    [req addPostValue:self.mp forKey:@"mp"];
    [req addPostValue:self.qujian_time forKey:@"qujian_time"];
    
    
    [req addPostValue:self.receiver_mp forKey:@"receiver_mp"];
    
    NSString* tmpImg=[NSString stringWithFormat:@"%@",self.serverPath];
    tmpImg=[tmpImg stringByAppendingString:@".jpg"];
    [req addPostValue:tmpImg forKey:@"avatar"];
    
    
    
    
    
    
    NSString*  uid=[ud objectForKey:@"uid"];
    
    [req addPostValue:[NSString stringWithFormat:@"%@",uid] forKey:@"uid"];
    [req startSynchronous];
    NSError *error = [req error];
    if (!error) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [MBProgressHUD hideHUDForView:app.window animated:YES];
        
        NSString *response = [req responseString];
        NSLog(@"保存返回结果%@",response);
        NSDictionary* dic=[response JSONValue];
        code=[[dic objectForKey:@"code"] intValue];
        [self performSelectorOnMainThread:@selector(finishFn) withObject:nil waitUntilDone:YES];
    }
    
}
-(void)finishFn{
    if (code==1) {
        [self.view makeToast:@"发布快递需求成功" duration:1.0 position:@"center"];
        self.receiver_mp=@"";
        self.road1=@"";
        self.road2=@"";
        
        
        self.weight=@"";
        self.qujian_time=@"";
        self.start_addr=@"";
        self.arrive_time=@"";
        
        self.length=@"";
        self.width=@"";
        self.height=@"";
        
        
        self.serverPath=@"";
        self.avatar=@"";
        self.bdImgPath=@"";
        [table reloadData];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
        
    }else{
        [self.view makeToast:@"发布快递需求失败" duration:1.0 position:@"center"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if(nicknameTF==textField){
        NSLog(@"self.textField==%@",textField.text);
        self.nickname=textField.text;
    }
    if (birthDateField==textField) {
        NSDate *date = datePicker.date;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyy-MM-dd"];
        NSString *dateStr = [formatter stringFromDate:date];
        textField.text = dateStr;
    }
    if (startAddrTF==textField) {
        self.start_addr=textField.text;
    }
    
    if (lengthTF==textField) {
        self.length=textField.text;
    }
    if (widhthTF==textField) {
        self.width=textField.text;
    }
    if (heightTF==textField) {
        self.height=textField.text;
    }
    if (receiveMpTF==textField) {
        self.receiver_mp=textField.text;
    }
    
    if (mpTF==textField) {
        self.mp=textField.text;
    }
    if (road1TF==textField) {
        self.road1=textField.text;
    }
    if (road2TF==textField) {
        self.road2=textField.text;
    }
    //    if (endAddrTF==textField) {
    //        self.end_addr=textField.text;
    //    }
    
    
    if (widhthTF==textField) {
        self.width=textField.text;
    }
    if (heightTF==textField) {
        self.height=textField.text;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"TodoViewController"];
    cell.tag = 1;
    if(!cell){
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TodoViewController"];
        
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    int row=indexPath.row;
    if (row==0) {
        int h=[Tools getScreenHeight];
        UILabel *addPicPreLb=[[UILabel alloc] initWithFrame:CGRectMake(7, 5, 80, 45)];
        addPicPreLb.text=@"头像:";
        addPicPreLb.font=[UIFont systemFontOfSize:14];
        addPicPreLb.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:addPicPreLb];
        
        
        
        UIButton *addPicBtn = [[UIButton alloc] initWithFrame:CGRectMake(87.0, 5, 0.8*167/2-2, 0.8*167/2-2)];
        
        [addPicBtn setBackgroundImage:[UIImage imageNamed:@"add_pic.png"] forState:0];
        [addPicBtn addTarget:self action:@selector(paiZhaoFn) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:addPicBtn];
        
        
        imgViewFrame = [[UIImageView alloc] initWithFrame:CGRectMake(87.0+99, 5, 0.8*167/2, 0.8*167/2)];
        
        
        
        if(myInfoDic){
            NSString*  avatar=[myInfoDic objectForKey:@"avatar"];
//            if(avatar&&avatar.length>5){
//                avatar=[HOST stringByAppendingString:avatar];
//                NSLog(@"头像=%@",avatar);
//                [imgViewFrame setImageWithURL:[NSURL URLWithString:avatar]];
//                
//            }
        }
        
        
        
        
        [cell.contentView addSubview:imgViewFrame];
        
        
        
        
        
        
    }
    if (row==1) {
        int h=[Tools getScreenHeight];
        
        
        
        NSString* typeStr=@"夜店中国号:";
        UILabel  *typePreLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 20, 80.0, 45)];
        [typePreLb setText:typeStr];
        typePreLb.font=[UIFont systemFontOfSize:14];
        typePreLb.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:typePreLb];
        
        
        
        
        NSString*  strUid=[myInfoDic objectForKey:@"uid"];
        UILabel* lb=[[UILabel alloc] initWithFrame:CGRectMake(87, 20, 180, 45)];
        lb.text=[NSString stringWithFormat:@"%@",strUid];
        
        lb.textColor=[UIColor blackColor];
        lb.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:lb];
        
        
        
        
    }
    
    if(row==2){
        UILabel *addPicPreLb=[[UILabel alloc] initWithFrame:CGRectMake(7, 5, 80, 45)];
        addPicPreLb.text=@"昵称:";
        addPicPreLb.font=[UIFont systemFontOfSize:14];
        addPicPreLb.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:addPicPreLb];
        //
        UIImageView*  bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 40)];
        bg.image=[UIImage imageNamed:@"2.png"];
        [cell.contentView  addSubview:bg];
        
        
        nicknameTF = [[UITextField alloc] initWithFrame:CGRectMake(87.0, 5, 200, 40)];
        if(myInfoDic){
            nickname=[myInfoDic objectForKey:@"nickname"];
            nicknameTF.text=nickname;
        }
        
        [cell.contentView addSubview:nicknameTF];
        
        
        
        
        
    }
    int y=7;
    if (row==3) {
        
        
        
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 70, 45)];
        
        nameLb.text=@"性别";
        nameLb.font=[UIFont systemFontOfSize:14];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        [cell.contentView  addSubview:nameLb];
        //
        UIImage* red=[UIImage imageNamed:@"red_dot.png"];
        
        UIButton* boyBtn=[[UIButton alloc] initWithFrame:CGRectMake(80, 5, 100, 45)];
        
        //
        UIImage* boy=[UIImage imageNamed:@"man"];
        UIImageView* boyView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 19/2-3, 16, 15)];
        boyView.image=boy;
        [boyBtn  addSubview:boyView];
        //
        UILabel* man=[[UILabel alloc] initWithFrame:CGRectMake(30, 5, 20, 20)];
        man.text=@"男";
        [boyBtn addSubview:man];
        //
        UIImageView* redView=[[UIImageView alloc] initWithFrame:CGRectMake(60, 19/2, 19/2, 19/2)];
        redView.image=red;
        
        [boyBtn addSubview:redView];
        [cell.contentView  addSubview:boyBtn];
        
        
        
        ////////
        UIImage* grey=[UIImage imageNamed:@"gray_dot.png"];
        
        UIButton* girlBtn=[[UIButton alloc] initWithFrame:CGRectMake(180, 5, 100, 45)];
        
        //
        UIImage* girl=[UIImage imageNamed:@"girl.png"];
        UIImageView* girlView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 19/2-3, 16, 15)];
        girlView.image=girl;
        [girlBtn  addSubview:girlView];
        //
        UILabel* girlLb=[[UILabel alloc] initWithFrame:CGRectMake(30, 5, 20, 20)];
        girlLb.text=@"女";
        [girlBtn addSubview:girlLb];
        //
        UIImageView* greyView=[[UIImageView alloc] initWithFrame:CGRectMake(60, 19/2, 19/2, 19/2)];
        greyView.image=grey;
        
        [girlBtn addSubview:greyView];
        
        [cell.contentView  addSubview:girlBtn];
        
        
        
    }
    
    if(row==4){
        
        
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 270, 45)];
        
        nameLb.text=@"生日";
        nameLb.font=[UIFont systemFontOfSize:14];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        
        [cell.contentView  addSubview:nameLb];
        
        int w=257-87-y+20;
        UIImageView*  bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 40)];
        bg.image=[UIImage imageNamed:@"2.png"];
        [cell.contentView  addSubview:bg];
        
        birthday=[myInfoDic objectForKey:@"birthday"];
        birthday=[NSString stringWithFormat:@"%@",birthday];
        birthDateField = [[UITextField alloc] initWithFrame:CGRectMake(87, 5, 200, 40)];
        //birthDateField.textColor = UIColorFromRGB(0x0d5fbb);
        //birthDateField.font = FONT(14);
        birthDateField.placeholder = @"请选择日期";
        if(birthday&&birthday.length>0){
            birthDateField.text=birthday;
        }
        birthDateField.delegate = self;
        birthDateField.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:birthDateField];
        
        
        
        
        
        
        
        
    }
    if(row==5){
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 270, 45)];
        
        nameLb.text=@"身高";
        nameLb.font=[UIFont systemFontOfSize:14];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        [cell.contentView  addSubview:nameLb];
        
        
        UIImageView*  bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 40)];
        bg.image=[UIImage imageNamed:@"2.png"];
        [cell.contentView  addSubview:bg];
        
        
        
        height=[myInfoDic objectForKey:@"height"];
        height=[NSString stringWithFormat:@"%@",height];
        heightTF=[[UITextField alloc] initWithFrame:CGRectMake(87+y, 5, 200-y, 40)];
        heightTF.placeholder=@"请输入身高";
        
        if(height.length>0){
            heightTF.text=height;
        }
        [cell.contentView addSubview:heightTF];
        
    }
    if(row==6){
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 270, 45)];
        
        nameLb.text=@"所在地";
        nameLb.font=[UIFont systemFontOfSize:14];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        [cell.contentView  addSubview:nameLb];
        
        
        UIImageView*  bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 50)];
        bg.image=[UIImage imageNamed:@"2.png"];
        [cell.contentView  addSubview:bg];
        
        
        
        
        self.addAddressBtn=[[UIButton alloc] initWithFrame:CGRectMake(87, 0, 200,50)];
        self.addAddressBtn.backgroundColor=[UIColor clearColor];
        [self.addAddressBtn addTarget:self action:@selector(showProvinceFn) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:self.addAddressBtn];
        //
        UILabel* selProvinceLb=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 50)];
        selProvinceLb.text=@"请选择省市";
        selProvinceLb.textColor=[UIColor grayColor];
        selProvinceLb.backgroundColor=[UIColor clearColor];
        [self.addAddressBtn addSubview:selProvinceLb];
        
        
        UIImage* arrowImg=[UIImage imageNamed:@"右箭头"];
        UIImageView*  arrowImgView=[[UIImageView alloc] initWithFrame:CGRectMake(280, (50-15)/2, 10, 15)];
        arrowImgView.image=arrowImg;
        [self.addAddressBtn addSubview:arrowImgView];
        
        
        
        
        
    }
    
    if(row==7){
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 270, 45)];
        
        nameLb.text=@"故乡";
        nameLb.font=[UIFont systemFontOfSize:14];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        [cell.contentView  addSubview:nameLb];
        
        
        UIImageView*  bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 50)];
        bg.image=[UIImage imageNamed:@"2.png"];
        [cell.contentView  addSubview:bg];
        
        
        
        
        self.hometownBtn=[[UIButton alloc] initWithFrame:CGRectMake(87, 0, 200,50)];
        self.hometownBtn.backgroundColor=[UIColor clearColor];
        [self.hometownBtn addTarget:self action:@selector(showHomeTownProvinceFn) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:self.hometownBtn];
        
        UILabel* selProvinceLb=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 50)];
        selProvinceLb.text=@"请选择省市";
        selProvinceLb.textColor=[UIColor grayColor];
        selProvinceLb.backgroundColor=[UIColor clearColor];
        [self.addAddressBtn addSubview:selProvinceLb];
        
        
        UIImage* arrowImg=[UIImage imageNamed:@"右箭头"];
        UIImageView*  arrowImgView=[[UIImageView alloc] initWithFrame:CGRectMake(280, (50-15)/2, 10, 15)];
        arrowImgView.image=arrowImg;
        [self.addAddressBtn addSubview:arrowImgView];
        
    }
    if(row==8){
        UILabel  *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5, 270, 45)];
        
        nameLb.text=@"个人简介";
        nameLb.font=[UIFont systemFontOfSize:15];
        nameLb.textAlignment=NSTextAlignmentLeft;
        nameLb.textColor=[UIColor blackColor];
        nameLb.backgroundColor = [UIColor clearColor];
        [cell.contentView  addSubview:nameLb];
        //
        UIImageView* bg=[[UIImageView alloc] initWithFrame:CGRectMake(87, 5, 200, 70)];
        bg.image=[UIImage imageNamed:@"textarea0825.png"];
        [cell.contentView  addSubview:bg];
        
        qmView=[[UITextView alloc] initWithFrame:CGRectMake(87+2, 5+2, 200-4, 70-4)];
        [cell.contentView  addSubview:qmView];
        
        
    }
    
    
    if (row==9){
        UIButton* btn= [[UIButton alloc] init];
        [btn addTarget:self action:@selector(saveOrderFn) forControlEvents:UIControlEventTouchUpInside];
        btn.frame=CGRectMake(60, 5, 200, 44);
        UIImage* img = [UIImage imageNamed: @"topbg"] ;
        [btn setBackgroundImage:img forState:UIControlStateNormal];
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
        label.text=@"提     交";
        label.textAlignment=NSTextAlignmentCenter;
        label.backgroundColor=[UIColor clearColor];
        label.textColor=[UIColor whiteColor];
        label.font=[UIFont boldSystemFontOfSize:20];
        [btn addSubview:label];
        [cell.contentView addSubview:btn];
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    return cell;
    
}


#pragma mark - touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [birthDateField resignFirstResponder];
    
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    
    if (!datePicker) {
        datePicker = [[UIDatePicker alloc] init];
        datePicker.frame = CGRectMake(0, 0, 320, 0);
        datePicker.datePickerMode = UIDatePickerModeDate;
    }
    textField.inputView = datePicker;
    return YES;
}






-(void)paiZhaoFn{
    
    
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        UIActionSheet *chooseImageSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
        [chooseImageSheet showInView:self.view];
        
    }else{
        // [[NSNotificationCenter defaultCenter] postNotificationName:@"paidanFn" object:nil];
        
        
        UIActionSheet *chooseImageSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
        [chooseImageSheet showInView:self.view];
    }
    
}
#pragma mark UIActionSheetDelegate Method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    switch (buttonIndex) {
        case 0://Take picture
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hideTab" object:nil];
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                
            }else{
                NSLog(@"模拟器无法打开相机");
            }
            [self presentModalViewController:picker animated:YES];
            break;
            
        case 1://From album
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hideTab" object:nil];
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentModalViewController:picker animated:YES];
            break;
            
        default:
            //[self dismissModalViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
            break;
    }
}

#pragma 拍照选择照片协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [UIApplication sharedApplication].statusBarHidden = YES;
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        CGSize newSize=CGSizeMake(160, 200);
        image=[self smallImg:image scaledToSize:newSize];
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到选择后沙盒中图片的完整路径
        NSString* filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
        self.avatar=filePath;
        
        
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
        
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        avatarImgView = [[UIImageView alloc] initWithFrame:
                         CGRectMake(0, 0, 167/2, 167/2)];
        
        avatarImgView.image = image;
        
        //加在视图中
        [imgViewFrame addSubview:avatarImgView];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
        
        [self savePhotoFn];
        
    }
}
-(void)savePhotoFn{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString* uploadURL=@"yw_uploadify.php";
    uploadURL=[HOST stringByAppendingString:uploadURL];
    
    //NSLog(@"uploadURL=%@",uploadURL);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:uploadURL]];
    
    NSDate* now = [NSDate date];
    NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
    fmt.dateFormat = @"yyyyMMddHHmmss";
    NSString* dateString = [fmt stringFromDate:now];
    NSLog(@"%@", dateString);
    
    
    self.serverPath=[Tools md5:dateString];
    
    
    
    
    [request setPostValue:self.serverPath forKey:@"serverImgName"];
    
    NSLog(@"serverPath=%@", self.serverPath);
    
    [request setFile:self.avatar  forKey:@"filename"];
    //[request setPostValue:serverPath forKey:@"path"];
    
    
    //[request setValue:uid forKey:@"uid"];
    request.delegate = self;
    //
//    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [[MBProgressHUD showHUDAddedTo:app.window animated:YES] setLabelText:@"正在处理中，请稍后..."];
    
    
    
    
    [request startAsynchronous];
    
    
    [self savrMyAvatarFn];
}
-(void)savrMyAvatarFn{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *uid=[userDefaults objectForKey:@"uid"];
    NSString* strFlag=[userDefaults objectForKey:@"flag"];
    int flag=[strFlag intValue];
    
    [self.view makeToast:@"照片保存成功!"
                duration:1.0
                position:[NSValue valueWithCGPoint:CGPointMake(160, 210)]
     ];
    NSDictionary* dic=[NSDictionary dictionaryWithObject:self.avatar forKey:@"avatar"];
    
    
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [MBProgressHUD hideHUDForView:app.window animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTab" object:nil];
    
    
    
    
}

-(UIImage*)smallImg:(UIImage*)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
